<?php

namespace app\common\controller;

use app\common\model\MoneyLog;
use app\common\model\GoodsNumberlog;
use EasyWeChat\Factory;
use Redis;
use Symfony\Component\Cache\Simple\RedisCache;
use think\Cache;
use think\Controller;
use think\Db;

use think\Paginator;


class Super extends Controller
{

    // 初始化
    protected function _initialize()
    {
        $this->resurl = config("oss.resurl");
        $this->assign("resurl", $this->resurl);
        parent::_initialize();
    }



    protected function getwxapp($type = "default")
    {
        $config = [
            'app_id' => config("wx.appid"),
            'secret' => config("wx.appsecret"),
            'mch_id' => config("wx.mch_id"),
            'token' => 'j4QQHR2NfkJOT4O7',
            'key' => config("wx.key"),   // API 密钥
            'cert_path'          => ROOT_PATH.'cert/apiclient_cert.pem', // XXX: 绝对路径！！！！
            'key_path'           => ROOT_PATH.'cert/apiclient_key.pem',      // XXX: 绝对路径！！！！
            'notify_url' => request()->domain() . "/api/order/notify",
            // 指定 API 调用返回结果的类型：array(default)/collection/object/raw/自定义类名
            'oauth' => [
                'scopes' => ['snsapi_userinfo'],
                'callback' => '/api/user/waplogin',
            ],
            'response_type' => 'array',
            'log' => [
                'level' => 'debug',
                'file' => '/tmp/wechat.log',
            ],
        ];
        $cache = Cache::init();
        $redis = $cache->handler();
        $cache = new RedisCache($redis);
        if ($type == "default") {
            $app = Factory::officialAccount($config);
        }

        if ($type == "pay") {
            $app = Factory::payment($config);
        }

        $app['cache'] = $cache;
        return $app;

    }

    protected function filterdata($data, $filterlist)
    {
        foreach ($filterlist as $key => $val) {
            if (empty($data[$key])) {
                e($filterlist[$key]);
            }
        }
    }



    protected function autodata($filter = [])
    {
        $data = input("post.");
        $_data = [];
        foreach (array_keys($data) as $key) {
            if (isset($data[$key])) {
                $_data[$key] = $data[$key];
            }
        }
        return $_data;
    }
    protected function getlevel($code){
        $group=model("group")->where("code",$code)->find();
        if(empty($group)){
            return false;
        }
        $next_number=intval($group['next_number']);
        $levellist=config("level");
        foreach ($levellist as $i=>$level){
            if($next_number>=$level['number']&&$next_number<$levellist[$i+1]['number'] && $i+1<count($levellist)){
                return $level;
            }
        }
        return false;
    }
    protected function setgroup_user_number($code){
        if($code==null){
            return false;
        }
        $count=model("user")->where("group_code",$code)->count();
        model("group")->where("code",$code)->update(['user_number'=>$count]);
        return true;
    }
    protected function setgroup_next_number($code){
        if($code==null){
            return false;
        }
        $next_number=model("group")->where("up_code",$code)->count();
        model("group")->where("code",$code)->update(['next_number'=>$next_number]);
        return true;
    }


}
